package com.szs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szs.common.CustomException;
import com.szs.common.R;
import com.szs.dto.DishDto;
import com.szs.mapper.DishMapper;
import com.szs.pojo.Category;
import com.szs.pojo.Dish;
import com.szs.pojo.DishFlavor;
import com.szs.service.CategoryService;
import com.szs.service.DishFlavorService;
import com.szs.service.DishService;
import com.szs.util.SystemConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public R<DishDto> saveWithFlavor(DishDto dishDto) {

        // 保存菜品的信息到dish表中
        this.save(dishDto);

        // 给

        Long dishId = dishDto.getId(); // 菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return dishDto;
        }).collect(Collectors.toList());

        // 保存菜品口味到dish_flavor表中
        dishFlavorService.saveBatch(dishDto.getFlavors());

        return R.success(dishDto);
    }

    @Override
    public R<Page> page(int page, int pageSize, String name) {

        // 创建分页构造器
        Page<Dish> pageInfo = new Page(page, pageSize);
        Page<DishDto> dtoPage = new Page();

        // 创建条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        // 根据名称模糊查询
        queryWrapper.like(name != null, Dish::getName, name);

        // 设置排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        // 执行查询
        super.page(pageInfo, queryWrapper);

        // 复制数据
        copy(pageInfo, dtoPage);

        return R.success(dtoPage);
    }


    // 对象数据拷贝
    private Page<DishDto> copy(Page<Dish> pageInfo, Page<DishDto> dtoPage) {
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list = records.stream().map((item) -> {

            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);

            // 获取分类信息
            Category category = categoryService.getById(item.getCategoryId());
            // 获取分类名称
            String name = category.getName();
            dishDto.setCategoryName(name);

            return dishDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);

        return dtoPage;
    }

    /**
     * 菜品删除
     *
     * @param ids
     * @return
     */
    @Override
    public R<String> remove(List<Long> ids) {

        // 删除菜品表中数据
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId, ids);
        queryWrapper.eq(Dish::getStatus, SystemConstant.ACCOUNT_STATUS_ON);
        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("菜品正在售卖中，不能删除！");
        }
        this.removeByIds(ids);
        // 删除菜品口味表中的数据
        LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorQueryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(dishFlavorQueryWrapper);
        return R.success("删除成功");
    }

    @Override
    public R<String> saleStatus(int type, List<Long> ids) {

        // 根据菜品id查询菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(Dish::getId, ids);

        List<Dish> list = this.list(queryWrapper);

        // 对选中的菜品进行售卖状态修改
        list = list.stream().map(item -> {
            Dish dish = new Dish();
            BeanUtils.copyProperties(item, dish, "status");
            if (type == SystemConstant.STATUS_OFF) {
                dish.setStatus(SystemConstant.STATUS_OFF);
            } else {
                dish.setStatus(SystemConstant.STATUS_ON);
            }
            return dish;
        }).collect(Collectors.toList());

        // 修改菜品的状态
        this.updateBatchById(list);

        return R.success("菜品状态修改成功！");
    }
}
